package com.example.exam.controller;

import com.example.exam.dto.LoginDTO;
import com.example.exam.entity.User;
import com.example.exam.service.UserService;
import com.example.exam.util.ResultVOUtil;
import com.example.exam.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author shishuqian
 * date 2021/5/12
 * time 16:17
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResultVO save(@RequestBody User user){
        User user1 = userService.findByUsername(user.getUsername());

        if (user1 != null){
           return ResultVOUtil.common(0,"注册失败，该用户名已被注册");
        }else {
            user.setCreateDate(new Date());
            userService.save(user);
           return ResultVOUtil.common(1,"注册成功");
        }

    }

    /**
     * 登录
     * @param loginDTO
     * @return token登录凭证
     */
    @PostMapping("/login")
    public ResultVO login(@RequestBody LoginDTO loginDTO){
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        //查询用户信息
        User user = userService.findByUsername(username);

        if (user != null){
            if (user.getPassword().equals(password)){
                return ResultVOUtil.success(user);
                //生成token，保存到数据库
//                String token = userService.createToken(user);
//                TokenVO tokenVO = new TokenVO();
//                tokenVO.setToken(token);
                //将token数据返回
//                return ResultVOUtil.success(tokenVO);
            }else {
                return ResultVOUtil.common(-1,"登录失败，密码错误");
            }
        }else {
            return ResultVOUtil.common(0,"登录失败，用户名错误");
        }
    }

    /**
     * 修改个人信息
     * @return
     */
    @PostMapping("/modify")
    public ResultVO modify(@RequestBody User user){
        int id = user.getId();
        User user1 = userService.findById(id);
        if (user1 ==null){
            return ResultVOUtil.error("用户不存在...");
        }else {
            user1.setUsername(user.getUsername());
            user1.setRealName(user.getRealName());
            user1.setPassword(user.getPassword());
            userService.save(user1);
            return ResultVOUtil.success(user1);
        }
    }

    @GetMapping("/findAll")
    public ResultVO findAll(){
        List<User> all = userService.findAll();
        return ResultVOUtil.success(all);
    }
    /**
     * 登出
     * @return
     */
    /*@PostMapping("/logout")
    public ResultVO logout(HttpServletRequest request){
        //从request中取出token
        String token = TokenUtil.getRequestToken(request);
        userService.logout(token);
        return ResultVOUtil.common(1,"退出成功");
    }*/

    @GetMapping("/test")
    public ResultVO test(String token){
        return ResultVOUtil.common(1,"验证成功，我可以返回数据给你");
    }
//    @PostMapping("/login")
//    public ResultVO login(@RequestBody User user){
//        User user1 = userService.findByUsername(user.getUsername());
//        if (user1 != null){
//            if (user1.getPassword().equals(user.getPassword())){
//               return ResultVOUtil.common(1,"登录成功!");
//            }else {
//               return ResultVOUtil.common(0,"登录失败,密码错误!");
//            }
//        }else {
//           return ResultVOUtil.common(0,"登录失败,用户名错误!");
//        }
//
//    }
//    @PostMapping("/login")
//    public String login(String username, String password){
//        Subject subject = SecurityUtils.getSubject();
//        //把用户填写的信息封装为token
//        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//
//        try {
//            subject.login(token);
//            //登录成功,取出信息放到session里
//            String name = token.getUsername();
//            subject.getSession().setAttribute("name",name);
//            return "index";
//        } catch (UnknownAccountException e) {
//            e.printStackTrace();
//            return "用户名错误!";
//        } catch (IncorrectCredentialsException e) {
//            e.printStackTrace();
//            return "密码错误!";
//        }
//
//    }

//    @GetMapping("/logout")
//    public String logout(){
//        Subject subject = SecurityUtils.getSubject();
//        //销毁session
//        subject.logout();
//        return "login";
//    }

}
